package com.ten_4_4.hiring.company.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.ten_4_4.hiring.comm.util.DBUtil;
import com.ten_4_4.hiring.company.po.CompanyPo;





/**
 * Company Dao
 *
 * @author Zzy.
 * @since 2016年10月12日
 */
public class CompanyDao {

	/**
	 * 企业登录
	 * 
	 * @param code
	 * @param password
	 * @return
	 * @throws RuntimeException
	 */
	public CompanyPo login(String code, String password) {
		String sql = "SELECT * FROM company WHERE code=? AND password=?";
		return DBUtil.getUniqueResult(CompanyPo.class, sql, code, password);
	}


	/**
	 * 企业注册
	 * 
	 * @param company
	 * @return
	 * @throws RuntimeException
	 */
	public int resiter(CompanyPo company) {
		String sql = "INSERT INTO company VALUES(NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		List<Object> paramList = new ArrayList<>();
		paramList.add(company.getCode());
		paramList.add(company.getPassword());
		paramList.add(company.getName());
		paramList.add(company.getEmail());
		paramList.add(company.getPhone());
		paramList.add(company.getLinkman());
		paramList.add(company.getAddress());
		paramList.add(company.getNumbers());
		paramList.add(company.getSlogan());
		return DBUtil.executeUpdate(sql, paramList);
	}
	
	/**
	 * 企业信息修改
	 * 
	 * @param company
	 * @return
	 * @throws RuntimeException
	 */
	public int update(CompanyPo company) {
		String sql = "UPDATE company SET code=?, password=?, name=?, email=?, phone=?, linkman=?, address=?, numbers=?, slogan=? WHERE id=?";
		List<Object> paramList = new ArrayList<>();
		
		paramList.add(company.getCode());
		paramList.add(company.getPassword());
		paramList.add(company.getName());
		paramList.add(company.getEmail());
		paramList.add(company.getPhone());
		paramList.add(company.getLinkman());
		paramList.add(company.getAddress());
		paramList.add(company.getNumbers());
		paramList.add(company.getSlogan());
		paramList.add(company.getId());
		
		return DBUtil.executeUpdate(sql, paramList);
		
	}

	public static CompanyPo findCompanyByEmail(String oldEmail) {
		String sql = "select id from company where email = ? ";
		CompanyPo companyPo = DBUtil.getUniqueResult(CompanyPo.class, sql, oldEmail);
		return companyPo;
	}

	public int modifyPassword(int companyId, String password) {
		String sql = "update company set password = ? where id = ?";
		return DBUtil.executeUpdate(sql, password, companyId);
	}



	public int modifyEmail(int companyId, String email) {
		String sql = "update company set email = ? where id = ?";
		return DBUtil.executeUpdate(sql, email, companyId);
	}

//	public List<CompanyPo> main() {
//		List<CompanyPo> companyList = new ArrayList<>();
//		Connection connection = DBUtil.getConnection();
//		PreparedStatement preparedStatement = null;
//		ResultSet resultSet = null;
//		try {
//			String sql = "SELECT * FROM company";
//			preparedStatement = connection.prepareStatement(sql);
//			resultSet = preparedStatement.executeQuery();
//			while (resultSet.next()) {
//				CompanyPo companyPo = new CompanyPo();
//				companyPo.setId(resultSet.getInt("id"));
//				companyPo.setCode(resultSet.getString("code"));
//				companyPo.setPassword(resultSet.getString("password"));
//				companyPo.setUsername(resultSet.getString("username"));
//				companyPo.setGender(resultSet.getInt("gender"));
//				companyList.add(companyPo);
//			}
//		} catch (Exception e) {
//			e.printStackTrace();
//			companyList = Collections.emptyList();
//		} finally {
//			DBUtil.close(resultSet, preparedStatement, connection);
//		}
//
//		return companyList;
//	}

//	public List<Sys_userEntity> list() {
//		List<Sys_userEntity> sys_userList = new ArrayList<>();
//		Connection connection = DBUtil.getConnection();
//		PreparedStatement preparedStatement = null;
//		ResultSet resultSet = null;
//		try {
//			String sql = "SELECT * FROM sys_user";
//			preparedStatement = connection.prepareStatement(sql);
//			resultSet = preparedStatement.executeQuery();
//			while (resultSet.next()) {
//				Sys_userEntity sys_userEntity = new Sys_userEntity();
//				sys_userEntity.setId(resultSet.getInt("id"));
//				sys_userEntity.setCode(resultSet.getString("code"));
//				sys_userEntity.setPassword(resultSet.getString("password"));
//				sys_userEntity.setUsername(resultSet.getString("username"));
//				sys_userEntity.setGender(resultSet.getInt("gender"));
//				sys_userEntity.setCreate_time(resultSet.getDate("create_time"));
//				sys_userEntity.setState(resultSet.getInt("state"));
//				sys_userList.add(sys_userEntity);
//			}
//		} catch (Exception e) {
//			e.printStackTrace();
//			sys_userList = Collections.emptyList();
//		} finally {
//			DBUtil.close(resultSet, preparedStatement, connection);
//		}
//
//		return sys_userList;
	
}